package com.douyu.ocean.recom.platfrom.core.web.controller;

import java.util.Collection;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.douyu.ocean.recom.platfrom.core.exception.BusinessException;
import com.douyu.ocean.recom.platfrom.core.exception.CommonErrorCode;
import com.douyu.ocean.recom.platfrom.core.web.utils.ResultUtils;
import com.douyu.ocean.recom.platfrom.core.web.vo.ResultWrapper;


public class BaseController {

	protected static Logger log = LoggerFactory.getLogger(BaseController.class);
	
	
	public Long getCurrentUserId(){
		Object userId = SecurityUtils.getSubject().getPrincipal();
		if(userId!=null){
			return Long.valueOf(userId.toString());
		}
		return null;
	}
	

	/*public static interface ActionHandler {
    	ResultWrapper<?> getResponse() throws Exception;
    }

	
	
	@SuppressWarnings("rawtypes")
    protected ResultWrapper<?> doHandler(ActionHandler handler, String actionName) {
        try {
            long current = System.currentTimeMillis();
            ResultWrapper<?> response = handler.getResponse();
            Object data = response.getResult();
            if (data instanceof Collection) {
                log.info("{}, result size:{}, cost:[{}ms]", actionName, ((Collection) data).size(),
                        System.currentTimeMillis() - current);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("{}, response data:{}, cost:[{}ms]", actionName, response.getResult(),
                            System.currentTimeMillis() - current);
                } else {
                    log.info("{}, cost:[{}ms]", actionName, System.currentTimeMillis() - current);
                }
            }
            return response;
        } catch (IllegalArgumentException e) {
            log.warn("catch illegal exception:", e);
            log.warn("{}, catch  IllegalArgumentException:{} {}", actionName, e, e.getMessage());
            return ResultUtils.asError(CommonErrorCode.PARAM_ERROR, e.getMessage());
        } catch (BusinessException e) {
            log.warn("catch  BusinessException:", e);
            log.warn("{}, catch  BussinessException:{} {}", actionName, e, e.getMessage());
            return ResultUtils.asError(e.getErrorCode(), e.getMessage());
        } catch (Exception e) {
            log.error("cat exception:", e);
            log.error("{}, catch  Exception:{} {}", actionName, e, e.getMessage());
            return ResultUtils.asError(CommonErrorCode.SYSTEM_ERROR, e.getMessage());
        }
    }*/
}
